
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;



public class Dialog extends JFrame implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 8182172081717792033L;

	private JList jlistWidth, jlistHeight;
	private JButton jbApply;
	
	public Dialog(String title ){
		super(title);

		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);// one must use the apply button
		
		JPanel jpBG = new JPanel();
	
		GridBagLayout gb = new GridBagLayout();
			jpBG.setLayout(gb);
		GridBagConstraints c = new GridBagConstraints();
			
			Insets defaultIns = new Insets(5,5,5,5);
			//Insets labelIns = new Insets(5,0,5,5);
			c.insets = defaultIns;
			
		JLabel jlTask = new JLabel("Please select the dimension of the forest ground.");
			c.gridwidth=3;
			gb.setConstraints(jlTask, c);
		jpBG.add(jlTask);
		
		JLabel jlHeight = new JLabel("Height:");
			c.gridwidth=1;
			c.gridy=1;
			gb.setConstraints(jlHeight, c);
		jpBG.add(jlHeight);
		
		JLabel jlWidth = new JLabel("Width:");
			c.gridx=1;
			gb.setConstraints(jlWidth, c);
		jpBG.add(jlWidth);
		
		DefaultListModel  modelDimension = new DefaultListModel();
	        modelDimension.addElement("300");
	        modelDimension.addElement("350");
	        modelDimension.addElement("400");
	        modelDimension.addElement("450");
	        modelDimension.addElement("500");
	        modelDimension.addElement("550");
	        modelDimension.addElement("600");
	        modelDimension.addElement("650");

	    //Create the lists and put them in a scroll pane.
	    jlistHeight = new JList(modelDimension);
	        jlistHeight.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	        jlistHeight.setSelectedIndex(0);
	        jlistHeight.setVisibleRowCount(6);
	        JScrollPane spHeight = new JScrollPane(jlistHeight);
	        	spHeight.setPreferredSize(new Dimension(100,100));
	        		c.gridx=0;
	        		c.gridy=2;
	        		c.gridheight=3;
	        		gb.setConstraints(spHeight, c);
	    jpBG.add(spHeight);
	    
	    jlistWidth = new JList(modelDimension);
	    	jlistWidth.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	    	jlistWidth.setSelectedIndex(0);
	    	jlistWidth.setVisibleRowCount(6);
	    	JScrollPane spWidth = new JScrollPane(jlistWidth);
	    		spWidth.setPreferredSize(new Dimension(100,100));
	    			c.gridx=1;
	    			gb.setConstraints(spWidth, c);
	    jpBG.add(spWidth);		
	    
	    jbApply = new JButton("apply");
	    	jbApply.addActionListener(this);
	    	c.gridx=2;
	    	c.gridy=4;
	    	c.anchor=GridBagConstraints.PAGE_END;
	    	c.gridheight=1;
	    	gb.setConstraints(jbApply, c);
	    jpBG.add(jbApply);
	    
	    this.setSize(400,250);
	    this.add(jpBG);
	    this.setVisible(true);
		
	}//constructor
	
	public int getWidthSelection(){
		return (this.jlistWidth.getSelectedIndex()*50+300);
	}//getWidthSelection
	
	public int getHeightSelection(){
		return (this.jlistHeight.getSelectedIndex()*50+300);
	}//getHeightSelection

	public void actionPerformed(ActionEvent aEv) {
		if(aEv.getSource()==jbApply){
			
			AntSimulation as=new AntSimulation(this.getWidthSelection(),this.getHeightSelection());
			this.setVisible(false);
		}
	}//actionPerformed(ActionEvent aEv)
	
}//class Dialog
